package com.zrrd.yunchmall.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.yunchmall.pojo.UserTmp;
import com.zrrd.yunchmall.user.mapper.UserMapper;
import com.zrrd.yunchmall.user.util.JwtUtil;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

/**
 * 执行登录认证的方法
 * @author yekong
 * @return 认证通过返回jwt令牌，失败作为返回null
 */
@Service
@SuppressWarnings("all")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserTmp> {


    //登录认证的方法
    public String login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        //生成的sql select uid, username, password... from t_user where username = ?
        //queryWrapper会被解析成一个具体的where条件
        UserTmp userTmp = getOne(queryWrapper);
        //判断用户是否存在
        if(userTmp != null) {
            if(BCrypt.checkpw(password, userTmp.getPassword())){
                return JwtUtil.create(30*60*1000, userTmp);
            }
        }
        //用户名或密码错误则返回空
        return null;
    }
}
